#ifndef __MV_HANDLER
#define __MV_HANDLER

//---------------------------------------------------------------------------
/*
   if ( !Read(...) && IsEof() )
     ConnectionAborted;
*/
CLASS( MVDataHandler )
  public:
    virtual ~MVDataHandler();

    virtual bool  Initialize( PHConfigItem cfg ) = 0;
    virtual bool  IsActive( void ) = 0;
    virtual bool  IsEOF( void ) = 0;
    virtual void  Close( void ) = 0;
    virtual DWORD Write( LPBYTE pBuff, DWORD dwLen ) = 0;
    virtual DWORD Read( LPBYTE pData, DWORD dwLen ) = 0;
    virtual void  GetCaption( MyString& s ) = 0;
};

//---------------------------------------------------------------------------
CLASSBASE( MVHandleHandler, public MVDataHandler )
  protected:
    virtual void  BeforeClose( void );
    virtual void  AfterRead( BOOL rc, DWORD sz );

  public:
    MVHandleHandler( void );

    virtual void  Close( void );
    virtual DWORD Write( LPBYTE pBuff, DWORD dwLen );
    virtual DWORD Read( LPBYTE pData, DWORD dwLen );
    virtual bool  IsActive( void );
    virtual bool  IsEOF( void );

  public:
    HANDLE Handle;
};

/*  Cfg params:
      CONSTSTR Port    "COM1"
      int      Speed   9600           300..115200
      int      Parity  NOPARITY       NOPARITY, ODDPARITY, EVENPARITY, MARKPARITY, SPACEPARITY
      int      Data    8              1..8
      int      Stop    ONESTOPBIT     ONESTOPBIT, ONE5STOPBITS, TWOSTOPBITS
*/
CLASSBASE( MVComPortHandeler, public MVHandleHandler )
    MyString Caption;
  protected:
    virtual void  BeforeClose( void );
    virtual void  AfterRead( BOOL rc, DWORD sz );

  public:
    MVComPortHandeler( void );

    virtual bool  Initialize( PHConfigItem cfg );
    virtual void  GetCaption( MyString& s );
};

/*  Cfg params:
      CONSTSTR File    ""
      bool     Cycle   false
*/
CLASSBASE( MVFileHandler, public MVHandleHandler )
  protected:
    MyString Caption;
    bool     isEof;
    bool     CycleFile;

  protected:
    virtual void  AfterRead( BOOL rc, DWORD sz );

  public:
    MVFileHandler( void );

    virtual bool  Initialize( PHConfigItem cfg );
    virtual bool  IsEOF( void );
    virtual void  GetCaption( MyString& s );
};

/*  Cfg params:
      int      LineSpeed   1000
*/
CLASSBASE( MVLineFileHandler, public MVFileHandler )
    bool   LineStart;
    DWORD  LastTime;

  protected:
    int  LineSpeed;
    bool HasLR;
    BYTE BackChar;

  public:
    MVLineFileHandler( void );

    virtual bool  Initialize( PHConfigItem cfg );
    virtual DWORD Read( LPBYTE pData, DWORD dwLen );
};

//---------------------------------------------------------------------------
#define MVH_FL_CONFIG  0x00000001UL

STRUCT( MVHandleInfo )
  CONSTSTR  Name;
  DWORD     Flags;

  /* TForm will be called:
       OnFormShow - to set fields from cfg
       OnFormHide - to set cfg from fields
       OnClose    - BEFORE form destroy

     Note: Sender will be PHConfigItem instead of TObject!
  */
  PTForm         __fastcall(__closure *ConfigForm)( void );
  PMVDataHandler __fastcall(__closure *CreateNew)( void );
};

#endif